[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

7. C Library

This chapter describes the C library usually provided with vbcc.


7.1 Introduction

To execute code compiled by vbcc, a library is needed. It provides basic interfaces to the underlying operating system or hardware as well as a set of often used functions.

A big part of the library is portable across all architectures. However, some functions (e.g. for input/output or memory allocation) are naturally dependent on the operating system or hardware. There are several sections in this chapter dealing with different versions of the library.

The library itself often is split into several parts. A startup-code will do useful initializations, like setting up IO, parsing the command line or initializing variables and hardware.

The biggest part of the functions will usually be stored in one library file. The name and format of this file depends on the conventions of the underlying system (e.g. `vc.lib' or `libvc.a').

Often, floating point code (if available) is stored in a different file (e.g. `m.lib' or `libm.a'). If floating point is used in an application, it might be necessary to explicitly link with this library (e.g. by specifying `-lm').

In many cases, the include files provide special inline-code or similar optimizations. Therefore, it is recommended to always include the corresponding include file when using a library function. Even if it is not necessary in all cases, it may affect the quality of the generated code.

The library implements the functions specified by ISO9899:1989 as well as a small number of the new functions from ISO9899:1999.


7.2 Legal

Most parts of this library are public domain. However, for some systems, parts may be under a different license. Please consult the system specific documentation. Usually, linking against this library will not put any restrictions on the created executable unless otherwise mentioned.


7.3 AmigaOS/68k

This section describes specifics of the C library for AmigaOS/68k. The relevant files are `startup.o', `minstart.o', `vc.lib', `vcs.lib', `mieee.lib', `m881.lib', `m040.lib', `amiga.lib', `amigas.lib', `extra.lib' and `extras.lib'.


7.3.1 Startup

The startup code currently consists of a slightly modified standard Amiga startup (`startup.o'). The startup code sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


7.3.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only. At the moment there are three math libraries:

`mieee.lib'
This one uses the C= math libraries. The startup code will always open MathIeeeSingBas.library, MathIeeeDoubBas.library and MathIeeeDoubTrans.library. Float return values are passed in d0, double return values are passed in d0/d1. A 68000 is sufficient to use this library. You must not specify `-fpu=...' when you use this library.
`m881.lib'
This one uses direct FPU instructions and function return values are passed in fp0. You must have a 68020 or higher and an FPU to use this library. You also have to specify `-fpu=68881'. Several FPU instructions that have to be emulated on 040/060 may be used.
`m040.lib'
This one uses only direct FPU instructions that do not have to be emulated on a 040/060. Other functions use the Motorola emulation routines modified by Aki M Laukkanen. It should be used for programs compiled for 040 or 060 with FPU. Return values are passed in fp0.

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


7.3.3 Stack

An application can specify the stack-size needed by defining a variable __stack (of type size_t) with external linkage, e.g.

 
size_t __stack=65536; /* 64KB stack-size */

The startup code will check whether the stack-size specified is larger than the default stack-size (as set in the shell) and switch to a new stack of appropriate size, if necessary.

If the `-stack-check' option is specified when compiling, the library will check for a stack overflow and abort the program, if the stack overflows. Note, however, that only code compiled with this option will be checked. Calls to libraries which have not been compiled with `-stack-check' or calls to OS function may cause a stack overflow which is not noticed.

Additionally, if `-stack-check' is used, the maximum stack-size used can be read by querying the external variable __stack_usage.

 
#include <stdio.h>

extern size_t __stack_usage;

main()
{
    do_program();
    printf("stack used: %lu\n",(unsigned long)__stack_usage);
}

Like above, the stack used by functions not compiled using `-stack-check' or OS functions is ignored.


7.3.4 Small data model

When using the small data model of the 68k series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries.

To compile and link a program using the small data model a command like

 
vc test.c -o test -sd -lvcs -lamigas

might be used.


7.3.5 Restrictions

The following list contains some restrictions of this version of the library:

tmpfile()
The tmpfile() function always returns an error.

clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process. For programs which cannot deal with this, a special version of clock() is provided in `extra.lib' (See section 7.3.9 extra.lib).


7.3.6 Minimal startup

If you want to write programs that use only Amiga functions and none from vc.lib you can use `minstart.o' instead of `startup.o' and produce smaller executables. This is only useful for people who know enough about the Amiga shared libraries, the stubs in amiga.lib etc. If you do not know enough about those things better forget minstart at all. This startup code does not set up all the things needed by vc.lib, so you must not use most of those functions (string and ctype funtions are ok, but most other functions - especially I/O and memory handling - must not be used). exit() is supplied by minstart and can be used. The command line is not parsed, but passed to main() as a single string, so you can declare main as int main(char *command) or int main(void). Also no Amiga libraries are opened (but SysBase ist set up), so you have to define and open DOSBase yourself if you need it. If you want to use floating point with the IEEE libraries you have to define and open MathIeeeSingBas.library, MathIeeeDoubBas.library and MathIeeeDoubTrans.library (in this order!) and link with mieee.lib (if compiled for FPU this is not needed). A hello world using minstart could look like this:

 
#include <exec/libraries.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
 
struct Library *DOSBase;
 
main()
{
    if(DOSBase=OpenLibrary("dos.library",0)){
        Write(Output(),"Hello, world!\n",14);
        CloseLibrary(DOSBase);
    }
    return 0;
}

This can yield an executable of under 300 bytes when compiled with `-sc -sd -O2' and linked with `minstart.o' and amigas.lib (using vlink - may not work with other linkers).


7.3.7 amiga.lib

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data. It is recommended to always use this library rather than the original version.

Additionally, there are header files (in the `proto'- and `inline'-subdirectories) which cause inlined calls to Amiga library functions.

For AmigaOS/68k, `amiga.lib' is linked by default.


7.3.8 auto.lib

To link with `auto.lib' (or the small data version `autos.lib') specify the `-lauto' or `-lautos' option to vc. When you are calling a standard Amiga library function and do not have defined the corresponding library base then the library base as well as code to open/close it will be taken from `auto.lib'. By default, `auto.lib' will try to open any library version. If you need at least a certain version you can define and set a variable _<library-base>Ver with external linkage, e.g. (on file-scope):

 
        int _IntuitionBaseVer = 39;

Note that your program will abort before reaching main() if one of the libraries cannot be opened. Also note that the dos.library will be openened by the standard startup code, not by auto.lib. This means you have to open dos.library yourself, when linking with `minstart.o'.


7.3.9 extra.lib

To link with `extra.lib' (or the small data version `extras.lib'), specify `-lextra' or `-lextras' to vc. There is also a header file `extra.h'. At the moment extra.lib contains the following functions:

getch()
Similar to getchar() but sets the console to raw mode and does not wait for return.
stricmp()
Case-insensitive variant of strcmp().
strnicmp()
Case-insensitive variant of strncmp().
chdir()
Changes the current directory.
clock()
This is an alternative version of the clock() function in `vc.lib'. clock() is supposed to return the amount of cpu-time spent by the current program. As this is generally not possible on the Amiga clock() from `vc.lib' always returns -1. However there seem to be several badly written programs that do not check this. So this version of clock() returns values which are strictly ascending for successive calls to clock() and the difference between them is the time difference between the calls.
iswhitespace()
Variant of isspace().

isseparator()
Tests for ',' or '|'.


7.3.10 ixemul


7.3.10.1 Introduction

ixemul.library is a shared Amiga library which is covered by the GNU license and includes standard ANSI and POSIX functions as well as some functions common in Unix, BSD and similar operating systems. Have a look at the ixemul-archives if you want to find out more. The link library ixemul.lib provided with vbcc only contains stub functions which call the functions in the shared library plus some auxiliary functions for vbcc. What are the main differences between vc.lib and ixemul.lib?

Things you should note:


7.3.10.2 Legal

The `crt0.o' and `ixemul.lib' in this archive are public domain. Have a look at the ixemul-archives to find out about ixemul.library.

7.3.10.3 Usage

First you must have the ixemul.library and the ixemul-includes (they should e.g. be on aminet in dev/gcc/ixemul-sdk.lha) and assign ixinclude: to the directory where they can be found. To compile a program to use ixemul you must make sure the proper config-file (`ixemul') is used, e.g.
 
        vc +ixemul hello.c
or

The small data model is currently not supported with `ixemul.lib', but the small code model is. If your program needs a certain version of the ixemul.library you can define a variable __ixemulVer with external linkage which specifies the minimum required version, e.g.
 
        int __ixemulVer = 45;


7.4 PowerUp/PPC

This section describes specifics of the C library for PowerUp/PPC. The relevant files are `startup.o', `minstart.o', `libvc.a', `libvcs.a', `libm.a', `libamiga.a', `libamigas.a', `libextra.a' and `libextras.a'.


7.4.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


7.4.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only.

The math library (`libm.a') is linked against the floating point library libmoto by Motorola.

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


7.4.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 7.3.3 Stack). The only difference is that stack-swapping cannot be done. If the default stack-size is less than the stack-size specified with __stack the program will abort.


7.4.4 Small data model

When using the small data model of the PPC series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries. To compile and link a program using the small data model a command like
 
vc test.c -o test -sd -lvcs -lamigas
might be used.


7.4.5 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process. For programs which cannot deal with this, a special version of clock() is provided in `extra.lib' (See section 7.3.9 extra.lib).


7.4.6 Minimal startup

The provided minimal startup code (`minstart.o') is used similarly like the one for 68k (See section 7.3.6 Minimal startup). Only use it if you know what you are doing.


7.4.7 libamiga.a

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement (`libamiga.a') automatically performs a necessary context switch to the 68k to execute the system call. Furthermore, it is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data.

Specify `-lamiga' to link with `libamiga.a'.


7.4.8 libauto.a

This library corresponds to the AmigaOS/68k version (See section 7.3.8 auto.lib).


7.4.9 libextra.a

This library corresponds to the AmigaOS/68k version (See section 7.3.9 extra.lib).


7.5 WarpOS/PPC

This section describes specifics of the C library for WarpOS/PPC. The relevant files are `startup.o', `vc.lib', `m.lib' `amiga.lib' and `extra.lib'.


7.5.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.


7.5.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only.

The math library (`m.lib') contains functions from Sun's portable floating point library. Additionally, there is a vbcc version of Andreas Heumann's `ppcmath.lib'. These routines are linked against Motorola's floating point routines optimized for PowerPC and therefore are much faster.

To make use of this library, link with `ppcmath.lib' before `m.lib', e.g.

 
    vc test.c -lppcmath -lm

Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


7.5.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 7.3.3 Stack).


7.5.4 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on AmigaOS it is not possible to obtain the time used by the calling process. For programs which cannot deal with this, a special version of clock() is provided in `extra.lib' (See section 7.3.9 extra.lib).


7.5.5 amiga.lib

To write programs accessing AmigaOS (rather than standard C functions only), a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement automatically performs a necessary context switch to the 68k to execute the system call. Furthermore, it is adapted to vbcc, does not cause collisions with some functions (e.g. sprintf) provided by the original `amiga.lib' and is available in small data. Specify `-lamiga' to link with `amiga.lib'.


7.5.6 auto.lib

This library corresponds to the AmigaOS/68k version (See section 7.3.8 auto.lib).

7.5.7 extra.lib

This library corresponds to the AmigaOS/68k version (See section 7.3.9 extra.lib).


7.6 MorphOS/PPC

This section describes specifics of the C library for MorphOS/PPC. The relevant files are `startup.o', `minstart.o', `libvc.a', `libvcs.a', `libm.a', `libamiga.a', `libamigas.a', `libextra.a' and `libextras.a'.

7.6.1 Startup

The startup code `startup.o' sets up some global variables and initializes stdin, stdout and stderr. The exit code closes all open files and frees all memory. If you link with a math library the startup/exit code will be taken from there if necessary.

7.6.2 Floating point

Note that you have to link with a math library if you want to use floating point. All math functions, special startup code and printf/scanf functions which support floating point are contained in the math libraries only. The math library (`libm.a') is linked against the floating point library libmoto by Motorola. Depending on the CPU/FPU selected, #including `math.h' will cause inline-code generated for certain math functions.


7.6.3 Stack

Stack-handling is similar to AmigaOS/68k (See section 7.3.3 Stack).


7.6.4 Small data model

When using the small data model of the PPC series CPUs, you also have to link with appropriate libraries. Most libraries documented here are also available as small data versions (with an 's' attached to the file name). Exceptions are the math libraries. To compile and link a program using the small data model a command like
 
vc test.c -o test -sd -lvcs -lamigas
might be used.


7.6.5 Restrictions

The following list contains some restrictions of this version of the library:
tmpfile()
The tmpfile() function always returns an error.
clock()
The clock() function always returns -1. This is correct, according to the C standard, because on MorphOS it is not possible to obtain the time used by the calling process. For programs which cannot deal with this, a special version of clock() is provided in `extra.lib' (See section 7.3.9 extra.lib).


7.6.6 libamiga.a

To write programs using AmigaOS compatible functions, a replacement for the original (copyrighted) `amiga.lib' is provided with vbcc. This replacement (`libamiga.a') will invoke the MorphOS 68k emulator to execute the system function. Furthermore, it is adapted to vbcc and does not cause collisions with some functions (e.g. sprintf) and is available in small data.

Specify `-lamiga' to link with `libamiga.a'.


7.6.7 libauto.a

This library corresponds to the AmigaOS/68k version (See section 7.3.8 auto.lib).

7.6.8 libextra.a

This library corresponds to the AmigaOS/68k version (See section 7.3.9 extra.lib).


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Volker Barthelmann on June, 26 2001 using texi2html